package com.itheima.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.ComboMapper;
import com.itheima.mapper.ComboTypeMapper;
import com.itheima.pojo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author 林晓伟
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class ComboService {
    @Resource
    private ComboMapper comboMapper;
    @Resource
    private ComboTypeMapper comboTypeMapper;

    /**
     * H17-新增套餐
     *
     * @param combo 新增套餐对象
     */
    public Integer add(Combo combo) {
        //获取套餐内的菜品集合
        List<Combotype> setmealDishes = combo.getSetmealDishes();
        //获取套餐内的分类id
        String categoryId = combo.getCategoryId();
        int i = Integer.parseInt(categoryId);
        //通过分类id获取分类中的菜品名字
        String v = comboMapper.v(i);
        //写入菜品名字
        combo.setCategoryName(v);
        //新增套餐
        comboMapper.add(combo);

        Integer id = combo.getId();
        String c = String.valueOf(id);

        Integer count = 0;
        if (setmealDishes == null) {
            return count;
        }
        //遍历菜品集合
        for (Combotype setmealDish : setmealDishes) {
            if (setmealDish == null) {
                continue;
            }
            //写入套餐id和套餐进行关联
            setmealDish.setSetmealId(c);
            comboMapper.adds(setmealDish);
            count++;
        }
        return count;
    }

    /**
     * H18-套餐分页查询
     *
     * @param page     页数
     * @param pageSize 每页条数
     * @param name     名字
     * @return Page<Combo>
     */
    public Page<Combo> select(Integer page, Integer pageSize, String name) {
        LambdaQueryWrapper<Combo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Combo::getName, name);

        Page<Combo> page1 = new Page<>(page, pageSize);
        if (name == null) {
            return comboMapper.selectPage(page1, null);
        }
        return comboMapper.selectPage(page1, lambdaQueryWrapper);
    }

    /**
     * H19-删除套餐
     *
     * @param id 删除套餐的id
     */
    public Integer delete(String id) {
        String[] split = id.split(",");
        int count = 0;
        if (split.length == 0) {
            return count;
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        for (String s : split) {
            if (s == null) {
                continue;
            }
            hashMap.put("setmealId", s);
            //根据id删除套餐
            comboMapper.deleteById(s);
            //根据id匹配setmealId删除套餐内的菜品
            comboTypeMapper.deleteByMap(hashMap);
            count++;
        }
        return count;
    }

    /**
     * H21-修改套餐
     *
     * @param combo 修改的对象
     */
    public Integer update(Combo combo) {
        //获取套餐内的菜品集合
        List<Combotype> setmealDishes = combo.getSetmealDishes();
        //获取套餐内的分类id
        String categoryId = combo.getCategoryId();
        int i = Integer.parseInt(categoryId);
        //通过分类id获取分类中的菜品名字
        String v = comboMapper.v(i);
        //写入菜品名字
        combo.setCategoryName(v);
        Integer id = combo.getId();
        String s = String.valueOf(id);
        //删除所有与此套餐相关的菜品
        comboMapper.dele(s);

        int count = 0;
        if (setmealDishes == null) {
            return count;
        }
        for (Combotype setmealDish : setmealDishes) {
            if (setmealDish == null) {
                continue;
            }
            //写入套餐id和套餐进行关联
            setmealDish.setSetmealId(s);
            //更改套餐
            comboMapper.update(combo);
            //写入所有菜品
            comboMapper.addd(setmealDish);
            count++;
        }
        return count;
    }

    /**
     * H20-根据id获取套餐
     *
     * @param id id
     * @return Combo
     */
    public Combo all(Integer id) {
        Combo combo = comboMapper.selectById(id);

        LambdaQueryWrapper<Combotype> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Combotype::getSetmealId, id);

        List<Combotype> combotypes = comboTypeMapper.selectList(lambdaQueryWrapper);
        combo.setSetmealDishes(combotypes);
        return combo;
    }

    /**
     * H22-套餐上架/下架
     *
     * @param status 状态值
     * @param ids    套餐id
     */
    public Integer up(Integer status, String ids) {
        String[] split = ids.split(",");

        int count = 0;
        if (split.length == 0) {
            return count;
        }
        for (String s : split) {
            if (s == null) {
                continue;
            }
            int i = Integer.parseInt(s);

            Integer ad = comboMapper.ad(i);
            if (ad != 0) {
                comboMapper.upd(i);
            } else {
                comboMapper.upds(i);
            }
            count++;
        }
        return count;
    }
}
